﻿using Api.Dal;
using Api.Model;

namespace Api.Bll
{
    public class PostBLL
    {
        public int count { get; set; }
        public Post GetPostById(int id)
        {
            return PostDAL.getPostById(id);
        }
        public List<Post> GetPost(Post post, bool isOrderByWeight)
        {
            List<Post> po = PostDAL.getPost(post, isOrderByWeight);
            return po;
        }


        public int InsertPost(Post post)
        {
            User u = UserDal.GetUserById(post.userId);
            if (u == null)
            {
                throw new Exception("用户不存在");
                return 0;
            }
            if (u.isForbidden == true)
            {
                Console.WriteLine("用户被禁言");
                return -1;
            }
            count++;
            post.id = count;
            return PostDAL.InsertPost(post);
        }
        public bool UpdatePost(Post post)
        {
            return PostDAL.UpdatePost(post);
        }
        public bool DeletePost(int pid)
        {
            bool u = PostDAL.DeletePostById(pid);
            if (u)
            {
                u = CommentDAL.DeleteCommentByPostId(pid);
            }
            return u;
        }

        public int likePost(int id)
        {
            return PostDAL.likePost(id);

            //return false;
        }

        public int dislikePost(int id)
        {
            return PostDAL.dislikePost(id);

        }

        //查看所有未审核的帖子
        public List<Post> getAllUnchecked()
        {
            return PostDAL.getAllUnchecked();
        }

        //帖子通过审核
        public bool pass(int id)
        {
            return PostDAL.pass(id);

        }
    }
}
